<template>
  <div class="company-name-container">
    <div class="company-item" v-for="(item,index) in companyNames" :class="{'text-right':companyNames.length===2&&index===0,'half-width': companyNames.length===2,'fixed-width': companyNames.length!==2}">
      <div class="side">{{index | sideFilter}}</div>
      <div class="company-name" v-if="!isEdit">{{item.name}}</div>
      <div class="company-name-input-box" v-else-if="useInput">
        <input type="text" class="company-name-input" :class="{'name-input2':companyNames.length===2}" v-model="names[index]">
      </div>
      <div class="company-options-box" v-else>
        <select class="name-select" :class="{'name-select2':companyNames.length===2}" v-model="names[index]">
          <option value="">请选择</option>
          <option v-for="optionItem in companyNames" :value="optionItem.id">{{optionItem.name}}</option>
        </select>
      </div>
    </div>
  </div>
</template>
<script type="text/javascript">
export default {
  props: {
    companyNames: Array,
    isEdit: {
      type: Boolean,
      default: false
    },
    useInput: {
      type: Boolean,
      default: false
    },
    isError: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      names: ['', '', '', '', '']
    }
  },
  mounted() {},
  watch: {
    companyNames(newVal) {
      if (this.isError) {
        this.names = []
        this.companyNames.forEach(item => {
          if (this.useInput) {
            this.names.push(item.name)
          } else {
            this.names.push(item.id)
          }
        })
      }
    }
  },
  filters: {
    sideFilter(v) {
      if (v === 0) {
        return '甲方'
      }
      if (v === 1) {
        return '乙方'
      }
      if (v === 2) {
        return '丙方'
      }
      if (v === 3) {
        return '丁方'
      }
      if (v === 4) {
        return '戊方'
      }
      return ''
    }
  }
}

</script>
<style type="text/css" scoped>
.company-name-container {
  white-space: nowrap;
}

.text-right {
  text-align: right;
}

.half-width {
  width: 50%;
}

.fixed-width {
  width: 470px;
  padding: 15px 100px !important;
}

.company-item {
  display: inline-block;
  padding: 15px 20px;
  font-size: 14px;
}

.side {
  color: #818181;
}

.company-name {
  margin-top: 15px;
  color: #01378c;
  font-weight: 700;
}

.name-select {
  display: inline-block;
  width: 100%;
  height: 30px;
  line-height: 30px;
  border: 1px solid #a5a5a5;
}

.name-select2 {
  width: 200px;
}

.company-name-input {
  display: inline-block;
  width: 100%;
  height: 30px;
  line-height: 30px;
  padding: 0 10px;
  border: 1px solid #a5a5a5;
}

.name-input2 {
  width: 200px;
}

</style>
